﻿using Common.Contract.Paging;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand
{
    public class RetrieveRawDataKHHDByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveRawDataKHHDByDaoRequest, RetrieveRawDataKHHDByDaoResponse>
    {
        #region Properties
        public string Dao;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public FilterRecord SearchColumns = null;
        #endregion
        public RetrieveRawDataKHHDByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveRawDataKHHDByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.Dao;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            SearchColumns = Request.Filter;
        }

        protected override bool OnExecute(RetrieveRawDataKHHDByDaoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<RawDataKhhdRecord> records = new List<RawDataKhhdRecord>();
                List<TblKpiRawKhhdDaily> listLoans = new List<TblKpiRawKhhdDaily>();
                listLoans = Providers.SalePerformance.RetrieveRawDataKHHDByDao(out totalRecord, Dao, SearchColumns, Request.Lang, pageSize, pageIndex);
                if (listLoans != null)
                {
                    records = listLoans.Select(x => new RawDataKhhdRecord
                               {
                                   Dao = x.Dao,
                                   BanGiao = x.BanGiao,
                                   LastYear = x.LastYear,
                                   NhanBanGiao = x.NhanBanGiao,
                                   SoKhhdTangThem = x.SoKhhdTangThem,
                                   SoLuyKe = x.SoLuyKe
                               }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
